package com.rocching.easychat.core;

import android.net.Uri;

import com.rocching.easychat.model.AppConst;
import com.rocching.easychat.model.UserModel;
import com.rocching.easychat.util.AppUtil;
import com.rocching.easychat.util.StringUtil;

import java.util.Objects;

import io.rong.imlib.model.UserInfo;

public class UserIdentityContext {
    private UserIdentityContext() {
    }

    private static UserModel userModel;

    public static void set(UserModel model) {
        userModel = model;
    }

    public static UserModel get() {
        return userModel;
    }

    public static void clear() {
        userModel = null;
    }

    public static void update(UserInfo info) {
        if (userModel != null) {
            userModel.setName(info.getName());
            userModel.setPhoto(info.getPortraitUri().getPath());
        }
    }

    public static void update(UserModel info) {
        if (userModel != null) {
            userModel.setName(info.getName());
            userModel.setPhoto(info.getPhoto());
            userModel.setRemark(info.getRemark());
            userModel.setPhone(info.getPhone());
        }
    }

    public static boolean isLogin() {
        return getUserId() > 0;
    }

    public static boolean isAdminUser() {
        String account = getAccount();
        if (StringUtil.isEmpty(account)) {
            return false;
        }
        return account.equalsIgnoreCase(AppConst.ADMIN_USER_ACCOUNT);
    }

    public static long getUserId() {
        if (Objects.isNull(userModel)) {
            return 0;
        }
        return userModel.getId();
    }

    public static String getUserName() {
        if (Objects.isNull(userModel)) {
            return "";
        }
        return userModel.getName();
    }

    public static String getAccount() {
        if (Objects.isNull(userModel)) {
            return "";
        }
        return userModel.getAccount();
    }

    public static String getPhoto() {
        if (Objects.isNull(userModel)) {
            return "";
        }
        return AppUtil.getUserPhoto(userModel.getPhoto());
    }

    public static Uri getPhotoUri() {
        String url = getPhoto();
        if (StringUtil.isEmpty(url)) {
            return null;
        }
        return Uri.parse(url);
    }

    public static String getUserIdString() {
        return AppUtil.getTargetUserId(getUserId());
    }
}
